package com.skt.common_hr.appmsnreq.model
{
	import com.skt.common_hr.appmsnreq.model.vo.voAppmList;
	import com.skt.common_hr.appmsnreq.model.vo.voAppmMain;
	import com.skt.common_hr.appmsnreq.model.vo.voAssign;
	import com.skt.common_hr.appmsnreq.model.vo.voGroup;
	import com.skt.common_hr.appmsnreq.model.vo.voPosition;
	import com.skt.common_hr.common.Variables;
	import com.skt.common_hr.common.puremvc.CommonProxy;
	import com.skt.common_hr.common.utilSilver.textSilver;
	import com.wcl.utils.DateUtil;
	
	import mx.collections.ArrayCollection;
	import mx.controls.Alert;
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;
	
	public class AppmsnreqProxy extends CommonProxy
	{
		public static const NAME:String = "AppmsnreqProxy";
		
		public var mainList:ArrayCollection;
		public var viewList:ArrayCollection;
		
		public var groupList:ArrayCollection;
		public var assignList:ArrayCollection;
		public var positionList:ArrayCollection;
		
		public var O_MREQ:String;
		public var outKey:String;
		
		public static var ins:AppmsnreqProxy;
		public static function getInstance():AppmsnreqProxy
		{
			if(ins==null)ins = new AppmsnreqProxy;
			return ins;	
		}
				
		/**
		 * 생성자
		 *
		 * @param
		 * @return void
		 */		
		public function AppmsnreqProxy(data:Object=null)
		{
			super(NAME, data);
		}
		
		
		/**
		 * main list
		 */
		public function loadList(dfStart:Date, dfEnd:Date, setFunc:Function):void
		{
			var obj:Object = {};
			
			obj.I_PERNR = Variables.myPernr;
			obj.I_BEGDA	= dfStart;
			obj.I_ENDDA	= dfEnd;		
			
			obj = toJsonType(obj);
			
			loadData("Z_HR_M001", obj, loadListResult, listFault, setFunc);
		}
		public function loadListResult(event:ResultEvent):void
		{
			if(retCode==0){
				mainList		=	parseMainList(event.result.OTAB);
				viewList		=	parseViewList(event.result.OTAB1);
				
				groupList		=	parseGroupList(event.result.T_CODE);
				assignList		=	parseAssignList(event.result.T_CODE1);
				positionList	=	parsePositionList(event.result.T_CODE2);
				
				O_MREQ			= 	event.result.O_MREQ;				
				retText 		= 	event.result.RETTEXT;
				
				event.result.func(mainList, viewList, groupList, assignList, positionList, O_MREQ, retText);
			}
			else{
				mainList = new ArrayCollection();
			}
		}
		
		/**
		 * 저장
		 */
		public function saveOrderList(params:Object, arItab:Array, arItab1:Array, setFunc:Function):void		//param과 table 함수호출 , sap에 저장
		{
			var tables:Object = new Object();																	//tables 생성
			
			tables["ITAB"]	=	arItab;																			
			tables["ITAB1"]	=	arItab1;
			
			saveData("Z_HR_M005", params, tables, saveOrderListResult, listFault, setFunc);						//savaData 호출하여 sap에 저장
		}
		public function saveOrderListResult(event:ResultEvent):void												//event = sap에서 넘어온 데이터
		{
			if(retCode==0) { 	//저장성공
				outKey = event.result["O_GWAREKEY"];
				event.result.func(outKey);
			}
			else { 				//저장실패
				Alert.show(retText);
			}
		}
		
		
				
		/**
		 * 대량 엑셀 업로드 
		 */
		public function saveOrderList_excel(params:Object, arItab1:Array, setFunc:Function):void		//param과 table 함수호출 , sap에 저장
		{
			var tables:Object = new Object();																	//tables 생성
			
			tables["ITAB"]	=	arItab1;
			
			saveData("Z_HR_M006", params, tables, saveOrderListResult_excel, listFault, setFunc);						//savaData 호출하여 sap에 저장
		}
		public function saveOrderListResult_excel(event:ResultEvent):void												//event = sap에서 넘어온 데이터
		{
			if(retCode==0) { 	//저장성공
				event.result.func();
			}
			else { 				//저장실패
				Alert.show(retText);
			}
		}
		
		/**
		 * 삭제
		 */
 		public function del(_GWAREKEY:String, setFunc:Function):void											//삭제는 결재키만 sap에 넘겨주면 됨.
		{
			var params:Object = new Object();																	
			params["I_GWAREKEY"] = _GWAREKEY;
			
			loadData("Z_HR_M004", params, delResult, listFault, setFunc);
		}
		public function delResult(event:ResultEvent):void
		{
			if(retCode==0) {	//삭제성공
				event.result.func();
			}
			else {				//삭제실패
				Alert.show('delResult err: ' + retText);
			}
		}
     
     
      private function parseMainList(recArr:ArrayCollection):ArrayCollection
      {
           var retArr:ArrayCollection = new ArrayCollection();

           for each(var obj:Object in recArr){
               
                var retObj:voAppmList = new voAppmList();
               
                for(var field:String in obj) {
	                if(retObj.hasOwnProperty(field)==false ) {
	                	continue;
	                }
	                if(field == "SDATE" || field == "APPDT") {
						retObj[field] = DateUtil.stringToDateTime(obj[field]);
						retObj[field+'_TX'] = DateUtil.toDefaultString(retObj[field]);
						retObj[field+'_JSON'] = DateUtil.toBasicString(retObj[field]);
					}
					else if(field == "PERNR") {
						retObj[field] = textSilver.pernrFunc(retObj[field]);
					}
					else{
						retObj[field] = obj[field];
					}
	             }
	                retArr.addItem(retObj);
	           }
	           return retArr;
	      }
     
      private function parseViewList(recArr:ArrayCollection):ArrayCollection
      {
           var retArr:ArrayCollection = new ArrayCollection();

           for each(var obj:Object in recArr){
               
                var retObj:voAppmMain = new voAppmMain();
               
                for(var field:String in obj) {
	                if(retObj.hasOwnProperty(field)==false ) {
	                	continue;
	                }
	                if(field == "SDATE" || field == "APPDT") {
						retObj[field] = DateUtil.stringToDateTime(obj[field]);
						retObj[field+'_TX'] = DateUtil.toDefaultString(retObj[field]);
						retObj[field+'_JSON'] = DateUtil.toBasicString(retObj[field]);
					}
					else if(field == "PERNR") {
						retObj[field] = textSilver.pernrFunc(obj[field]);
					}
					else{
						retObj[field] = obj[field];
						}
	                }
	                retArr.addItem(retObj);
	           }
	           return retArr;
	      }
     
	    private function parseGroupList(recArr:ArrayCollection):ArrayCollection
	      {
	           var retArr:ArrayCollection = new ArrayCollection();
	
	           for each(var obj:Object in recArr){
	               
	                var retObj:voGroup = new voGroup();
	             
	                for(var field:String in obj) {
	                     if(retObj.hasOwnProperty(field)==false ) {
	               			continue;
	                     }
	                     retObj[field] = obj[field];
	                }
	                retArr.addItem(retObj);
	           }
	           return retArr;
	      }
     
	     private function parseAssignList(recArr:ArrayCollection):ArrayCollection
	      {
	           var retArr:ArrayCollection = new ArrayCollection();
	
	           for each(var obj:Object in recArr){
	               
	                var retObj:voAssign = new voAssign();
	             
	                for(var field:String in obj) {
	                     if(retObj.hasOwnProperty(field)==false ) {
	                		continue;
	                     }
	                     retObj[field] = obj[field];
	                }
	                retArr.addItem(retObj);
	           }
	           return retArr;
	      }
     
	     private function parsePositionList(recArr:ArrayCollection):ArrayCollection
	      {
	           var retArr:ArrayCollection = new ArrayCollection();
	
	           for each(var obj:Object in recArr){
	               
	                var retObj:voPosition = new voPosition();
	             
	                for(var field:String in obj) {
	                     if(retObj.hasOwnProperty(field)==false ) {
	                		continue;
	                     }
	                     retObj[field] = obj[field];
	                }
	                retArr.addItem(retObj);
	           }
	           return retArr;
	      }
      
      	private function toJsonType(vo:Object):Object {
			var retObj:Object = new Object;
			for(var field:String in vo) {
				if(vo[field] is Date) {
					retObj[field] = DateUtil.toBasicString(vo[field]);
				}
				else {
					retObj[field] = vo[field];
				}
				trace(retObj[field]);
			} 
			return retObj;
		}

		private function listFault(event:FaultEvent):void
		{
			Alert.show('HthIcProxy.as listFault err: ' + retText);
		}

	}
}